package zeno.project.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 配置swagger2在线接口文档
 * @author zhiyong
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static final String API_TITLE = "Zeno学习spring boot的项目后端API";
    private static final String API_DESCRIPTION = "系统后台REST接口定义需参考该接口规范。admin-!qAz@wSx";
    private static final String API_LICENSE = "版权所有 2021 广州Zeno品牌信息科技有限公司。";
    private static final String API_LICENSE_URL = "https://www.bilibili.com/";
    private static final String API_VERSION = "1.0.0-SNAPSHOT";

    private static final String TOKEN_HEADER = "Authorization";
    private static final String TOKEN_TYPE = "Bearer";
    private static final String API_BASE_PACKAGE = "zeno.project.springboot.controller";

    @Bean
    public Docket coreDataApi() {
        return this.createRestApi("Core Management", API_BASE_PACKAGE + ".core", true);
    }

//    @Bean
//    public Docket tokenDataApi() {
//        return this.createRestApi("Token Management", API_BASE_PACKAGE + ".token", false);
//    }

    private Docket createRestApi(String groupName, String basePackage, Boolean requireToken) {
        // 使得每个接口有一个公共参数，不需要在每个接口单独配置
//        ParameterBuilder tokenPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<>();
//        tokenPar.name(TOKEN_HEADER)
//                .description("JWT令牌")
//                .modelRef(new ModelRef("string"))
//                .parameterType("header")
//                .required(requireToken)
//                .defaultValue(TOKEN_TYPE + " ").build();
//        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
//                .globalOperationParameters(pars);
    }

    /**
     * swagger页面的基本信息设置
     * @return api
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(API_TITLE)
                .description(API_DESCRIPTION)
                .termsOfServiceUrl("API网关接口：http://www.funtl.com")
                .license(API_LICENSE)
                .licenseUrl(API_LICENSE_URL)
                .version(API_VERSION)
                .build();
    }
}
